package Q2_02_Return_Kth_To_Last; import CtCILibrary.*; public class QuestionD { public static LinkedListNode nthToLast(LinkedListNode head, int k) { LinkedListNode p1 = head; LinkedListNode p2 = head; /* Move p1 k nodes into the list.*/ for (int i = 0; i < k; i++) { if (p1 == null) return null; // Out of bounds p1 = p1.next; } /* Move them at the same pace. When p1 hits the end, * p2 will be at the right element. */ while (p1 != null) { p1 = p1.next; p2 = p2.next; } return p2; } public static void main(String[] args) { int[] array = {0, 1, 2, 3}; LinkedListNode head = AssortedMethods.createLinkedListFromArray(array); for (int i = 0; i <= array.length + 1; i++) { LinkedListNode node = nthToLast(head, i); String nodeValue = node == null ? "null" : "" + node.data; System.out.println(i + ": " + nodeValue); } } }